import pyTFC
import random


def state_display(display_cube_state):
    for i, j in enumerate(display_cube_state):
        if j > 3:
            display_cube_state[i] -= 3

    print(f"雷立方状态：{display_cube_state}")


def decryption(decryption_cube_state):
    # 稻妻雷立方解密
    # 资料：https://www.miyoushe.com/ys/article/7816528

    knock_count = [0, 0, 0, 0]
    shift = []
    max_num = max(decryption_cube_state)

    print(f"原始状态：{decryption_cube_state}\n")

    # 寻找偏移量
    for item in decryption_cube_state:
        shift.append(max_num - item)

    print("敲击方法：")
    for index, value in enumerate(shift):
        match index:
            case 0:
                knock_count[3 - 1] += value * 2
            case 1:
                knock_count[1 - 1] += value
                knock_count[3 - 1] += value
            case 2:
                knock_count[2 - 1] += value
                knock_count[4 - 1] += value
            case 3:
                knock_count[2 - 1] += value * 2

    for index, value in enumerate(knock_count):
        if value > 3:
            value -= 3
        if value != 0:
            print(f"敲击第{index+1}个{value}次")
    print("")

    print("详细步骤：")
    for index, value in enumerate(knock_count):
        if value > 3:
            value -= 3
        if value != 0:
            print(f"敲击第{index+1}个{value}次")
            for i in range(value):
                play_ray_cube(index, decryption_cube_state)

    print(f"\n最终结果：{decryption_cube_state}")


def play_ray_cube(location, cube_state):
    cube_state[location] += 1
    try:
        if location - 1 >= 0:
            cube_state[location - 1] += 1
    except IndexError:
        pass

    try:
        cube_state[location + 1] += 1
    except IndexError:
        pass

    state_display(cube_state)


def random_solution(random_cube_state):
    times = 0

    state_display(random_cube_state)

    while True:
        random_num = random.randint(1, 3)

        times += 1
        print(f"敲击第{random_num}个")
        play_ray_cube(random_num - 1, random_cube_state)

        if (random_cube_state[0] == random_cube_state[1]
                and random_cube_state[1] == random_cube_state[2]
                and random_cube_state[2] == random_cube_state[3]):
            print(f"解密完成，总共敲击了{times}下")
            break


if __name__ == "__main__":
    # 稻妻雷立方
    count = 0
    original_state = []
    for item in range(4):
        original_state.append(random.randint(1, 3))
    state = original_state[:]
    menu_list = ["敲击第一个", "敲击第二个", "敲击第三个", "敲击第四个", "继续解", "显示完整解法", "随机数解法"]

    state_display(state)
    while True:
        if (state[0] == state[1]
                and state[1] == state[2]
                and state[2] == state[3]):
            print(f"解密完成，总共敲击了{count}下")
            break
        choose = pyTFC.Console.menu_display(menu_list)
        match choose:
            case 0:
                break
            case 5:
                # 继续解
                decryption(state)
                break
            case 6:
                # 从头开始解
                decryption(original_state)
                break
            case 7:
                # 随机数解法
                random_solution(original_state)
                break
            case 1 | 2 | 3 | 4:
                count += 1
                play_ray_cube(choose - 1, state)
            case _:
                print("请重新输入")
